Skip to content

feat: add API key spending quota#106

Merged
g1331 merged 6 commits intomasterfrom
add-api-key-spending-quota
Mar 20, 2026
Merged

feat: add API key spending quota#106
g1331 merged 6 commits intomasterfrom
add-api-key-spending-quota

Conversation

@g1331
Copy link
Owner

@g1331 g1331 commented Mar 20, 2026

Summary

为 API Key 增加按金额限制的限额能力,支持与上游消费规则一致的时间窗口配置,并在超过额度时进行硬拒绝。

本次提交同时补上两类回归修复:

  1. 保留 unbilled 请求的放行语义,避免在无法计费的场景中被额度短路拦截。
  2. 将 API Key 创建、更新、删除后的 quota 状态同步改为尽力而为处理,避免数据库事务已经提交后因为后续同步失败而向调用方返回 500。

Related Issue

N/A

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (code improvement without changing functionality)
  • Tests (adding or updating tests)
  • Build/CI (changes to build system or CI configuration)

Changes

  1. 新增 API Key 金额限额的数据结构、额度跟踪服务与管理接口字段,覆盖创建、编辑、展示与请求前校验。
  2. 复用现有消费规则窗口语义,支持小时、天与滚动窗口等配置方式,并将超额场景统一处理为硬拒绝。
  3. 在代理请求侧补充前置拒绝与计费判定衔接逻辑,仅对可明确计费的请求执行额度短路,保留 model_missingusage_missingprice_not_foundunbilled 场景的既有行为。
  4. 在 key manager 中为 create、update、delete 的 quota 状态同步增加回退逻辑,避免提交成功后的附加同步失败影响接口返回。
  5. 补充管理端组件、API、转换器与服务层测试,覆盖额度配置、前置拒绝、unbilled 例外以及 post-commit best-effort 行为。

Test Plan

  • Local tests pass (pnpm test:run)
  • Type check passes (pnpm exec tsc --noEmit)
  • Lint passes (pnpm lint)
  • Manual testing completed

Checklist

  • Code follows the project's coding standards
  • Tests have been added where necessary
  • Documentation has been updated (if applicable)
  • Changes do not introduce security vulnerabilities
  • Commit messages follow conventions

Screenshots

N/A

Additional Notes

pnpm test:run 已完整通过,结果为 2033 passed | 1 skipped。本 PR 已包含针对评审意见中两处行为回退的修复与对应回归测试。

@codecov
Copy link

codecov bot commented Mar 20, 2026

Codecov Report

❌ Patch coverage is 59.14787% with 163 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.57%. Comparing base (278cf6b) to head (3ed4352).
⚠️ Report is 7 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #106      +/-   ##
==========================================
- Coverage   76.35%   75.57%   -0.79%     
==========================================
  Files          97       99       +2     
  Lines        6982     7319     +337     
  Branches     2438     2557     +119     
==========================================
+ Hits         5331     5531     +200     
- Misses        922      994      +72     
- Partials      729      794      +65     
Flag Coverage Δ
typescript 75.57% <59.14%> (-0.79%) ⬇️
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…ions

- Simplify header: replace Card wrapper with compact inline toolbar
- Add expandable row pattern for quota details with enter animation
- Redesign quota rules as responsive horizontal grid (1/2/3 columns)
- Replace status Badge with compact color dot indicator
- Add grid-rows animation for edit dialog section transitions
- Upstream selection and rolling period fields animate on toggle
- Update tests to match new expandable row behavior
@g1331 g1331 merged commit bd7d040 into master Mar 20, 2026
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant